package com.northpool.operator.common;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import com.northpool.operator.common.database.NacosConfigReader;

import java.util.Properties;
import java.util.concurrent.Executor;

public class NacosConfigListenerExample {
    private static final String SERVER_ADDR = "192.168.164.45:8848";
    private static final String DATA_ID = "";
    private static final String GROUP = "dev";

    public static void main(String[] args) {
        try {
            Properties properties = new Properties();
            properties.put("serverAddr", SERVER_ADDR);
            properties.put("namespace", "dev");
            properties.put("username", "nacos");
            properties.put("password", "admin");
            properties.put("group", "dev");

            ConfigService configService = NacosFactory.createConfigService(SERVER_ADDR);
            String config = configService.getConfig(null, GROUP, 5000);
            System.out.println("Initial Config: " + config);
            configService.addListener(null, GROUP, new Listener() {
                @Override
                public Executor getExecutor() {
                    return null;
                }

                @Override
                public void receiveConfigInfo(String configInfo) {
                    System.out.println("Config changed: " + configInfo);
                }
            });

            // 保持主线程运行
            Thread.sleep(Long.MAX_VALUE);
        } catch (NacosException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
